﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp_SmartParking_Base;
using WpfApp_SmartParking_Entities;
using WpfApp_SmartParking_IService;
using WpfApp_SmartParking_Models;

namespace WpfApp_SmartParking.ViewModels.Pages;

public class UserManagementViewModel:ViewModelBase
{
    public ObservableCollection<UserModel> Users { get; set; } = new ObservableCollection<UserModel>();

    private readonly IUserService _userService;

    private readonly IDialogService _dialogService;

    private readonly IEventAggregator _eventAggregator;

    public UserManagementViewModel(
        IRegionManager regionManager,
        IUserService userService,
        IDialogService dialogService,
        IEventAggregator eventAggregator) :base(regionManager)
    {
        PageTitle = "用户管理";
        IsCanClose = true;
        _userService = userService;
        _dialogService = dialogService;
        _eventAggregator = eventAggregator;

        _eventAggregator.GetEvent<RefreshUserEvent>().Subscribe(() => 
        {
            InitUser();
        },true);

        OpenMenuDetailWindowCommand = new DelegateCommand<Object?>(DoOpenDetailWindow);

        InitUser();

    }

    private void InitUser()
    {
        Users.Clear();
        List<SysUser> sysUsers = _userService.GetUsers(SearchKey).ToList();
        sysUsers.ForEach(_user =>
        {
            UserModel userModel = new UserModel();
            userModel.Name = _user.Name;
            userModel.Id = _user.Id;
            userModel.Password = _user.Password;
            userModel.Address = _user.Address;
            userModel.Phone = _user.Phone;
            userModel.Mobile = _user.Mobile;
            userModel.Gender = _user.Gender;
            userModel.Qq = _user.Qq;
            userModel.Email = _user.Email;
            userModel.CompanyId = _user.CompanyId;
            userModel.Status = _user.Status;
            userModel.Avatar = $"pack://siteoforigin:,,,/Avatars/{_user.Avatar}";
            Users.Add(userModel);
        });
    }

    /// <summary>
    /// 打开详情窗口
    /// </summary>
    /// <param name="detail"></param>
    private void DoOpenDetailWindow(Object? detail)
    {
        IDialogParameters dialogParameters = new DialogParameters();
        dialogParameters.Add("Detail",detail);
        _dialogService.ShowDialog("UserDetailView", dialogParameters, _result => 
        {
            
        });
    }
}
